class Solution {
public:
    int strToInt(string str) {
        if(str.empty())
        {
            return 0;
        }
        int i=0,sign=1;
        int res=0,bnary=INT_MAX/10;
        while(str[i]==' ')//跳过空格
        {
            ++i;
        }
        if(str[i]=='-'||str[i]=='+')//判断正负
        {
            sign=str[i]=='-'?-1:1;
            ++i;
        }
        for(int j=i;j<str.size();++j)
        {
            if(str[j]<'0'||str[j]>'9')//非数字跳出
            {
                break;
            }
            if(res>bnary||res==bnary&&str[j]>'7')//越界判断
            {
                return sign==1?INT_MAX:INT_MIN;
            }
            res=res*10+(str[j]-'0');
        }
        return res*sign;
    }
};